package Controller;

import java.util.ArrayList;

import IDbConnections.SourceDatabase;
import Model.BusinessRule;
import Model.BusinessRuleManager;
import Model.BusinessRules.IRuleTypes;
import Model.Content.LanguageList;
import Model.Content.RuleTypeList;
import Model.Languages.Template;
import View.MyFrame;

public class Controller{

	private Template templatecopy;
	private SourceDatabase sourcedatabase;
	private BusinessRuleManager BRList;
	private MyFrame view;
	
	public Controller(MyFrame v, BusinessRuleManager B){
		view = v;
		BRList = B;
		
		sourcedatabase = new IDbConnections.SourceDatabase();
    	sourcedatabase.getBusinessRules(BRList);
    	
    	new ActionEvents(this, v, BRList);
    	
    	
	}
	
	public String executeGenerator(){
		ArrayList<BusinessRule> buslist = BRList.getBusinessToGenerate();
		try{
			String text = "";
			for(BusinessRule b : buslist){
				Template generatethis = templatecopy;
				IRuleTypes ruletype = RuleTypeList.getRule(b.getType());
				if(ruletype != null){
					generatethis.setRuleType(ruletype);
					
					text += "\n\n\n" + generatethis.Generate(b);
					
				} else {
					text += "\n\n\n-- !!!----------------------!!!\n";
					text += "-- !!! " + b.getType() + " : " + b.getBusinessRuleName() + " !!!\n";
					text += "-- !!! Couldn't generated because the rule type is unknown !!!\n";
					text += "-- !!!----------------------!!!\n\n\n";
				} 
			}
		
			return text;
		} catch (Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	
	
	
	public void setGenerateLanguage(String lang){
		try{
			Template check = LanguageList.getTemplate(lang);
			if(check != null){
				templatecopy = check;
			} else {
				throw new Exception("Cannot find the template");
			}
		} catch (Exception e){
			e.printStackTrace();
		}

	}
	
	public boolean setAllToSelectList(){
		return BRList.SetAllToSelect();
	}
	
	public boolean setAllToGenerate(){
		return BRList.SetAllToGenerate();
	}
	
	
	
}
